package entities;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
import rest.xstream.ChannelIdConverter;

import javax.persistence.*;
import java.util.Date;

/**
 * News entity class
 * User: kaiser
 * Date: 7/31/13
 * Time: 9:14 PM
 */

/*
<news>
       <item id="1" channel="habra" link="http://....." channel="habra" rank="2">Develop Android Application</item>
        <item id="2" channel="habra" link="http://....." channel="habra" rank="1">Google+</item>
   <channel>
<news>
 */

@Entity
@XStreamAlias("item")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = {"title"})
public class News {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    /**
     * News channel
     */
    @XStreamAsAttribute
    @XStreamConverter(ChannelIdConverter.class)
    private Channel channel;

    /**
     * News details url
     */
    @XStreamAlias("link")
    protected String url;

    /**
     * News user rank
     */
    protected int rank;

    /**
     * News title
     */
    protected String title;

    /**
     * News published date
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    @XStreamOmitField
    protected Date published;

    public News() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @ManyToOne
    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public Date getPublishedDate() {
        return published;
    }

    public void setPublishedDate(Date published) {
        this.published = published;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", channel=" + channel +
                ", url='" + url + '\'' +
                ", rank=" + rank +
                ", title='" + title + '\'' +
                ", published=" + published +
                '}';
    }
}